/**
 * created by 小卷毛, 2019-09-23 023
 * Copyright (c) 2019, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.af.community.smart.viewmodel

import android.content.Context
import com.af.community.smart.model.CityData
import com.af.community.smart.model.PeopleData
import com.af.community.smart.model.PeopleModel
import com.af.community.smart.model.ResultData
import com.af.community.smart.share.BaseUrl
import com.af.community.smart.utils.applyFlowableSchedulers
import com.af.community.smart.utils.applySchedulers
import com.af.community.smart.utils.getJsonFromAssets
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.lzg.extend.BaseResponse
import com.lzg.extend.JsonConvert
import com.lzg.extend.jackson.JacksonConvert
import com.lzg.okrx.RxSingleHelper
import com.lzg.okrx.adapter.FlowableBody
import com.lzy.okgo.OkGo
import io.reactivex.Flowable
import io.reactivex.Single
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

/**
 * 项目名称：Smart_Business
 * 创建人：小卷毛
 * 创建时间：2019-09-25 14:40
 */
class PeopleViewModel : DialogViewModel() {

    fun getGridList(): Flowable<ArrayList<ResultData>> =
        OkGo.post<BaseResponse<ArrayList<ResultData>>>(BaseUrl.gridList)
            .converter(object : JsonConvert<BaseResponse<ArrayList<ResultData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }
            .map { it.data }

    fun getPeopleList(vararg params: Pair<String, Any>): Flowable<ArrayList<PeopleData>> =
        OkGo.post<BaseResponse<PeopleModel>>(BaseUrl.userList)
            .params(getParams(*params))
            .isMultipart(true)
            .converter(object : JacksonConvert<BaseResponse<PeopleModel>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data.list }

    fun getAddData(vararg params: Pair<String, Any>): Single<String> =
        RxSingleHelper.requestString(
            url = BaseUrl.userAdd,
            isMultipart = true,
            params = getParams(*params)
        )
            .compose(applySchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }

    fun getCityData(context: Context) =
        Flowable.just("province_data.json")
            .subscribeOn(Schedulers.io())
            .map {
                val json = context.getJsonFromAssets("province_data.json")
                if (json.isEmpty()) return@map ArrayList<CityData>()
                return@map Gson().fromJson(
                    json,
                    object : TypeToken<List<CityData>>() {}.type
                ) as ArrayList<CityData>
            }
            .observeOn(AndroidSchedulers.mainThread())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }

}